﻿using SqlSugar;
using AhCha.Fortunate.Common.Extensions;
using AhCha.Fortunate.Common.Global;

namespace AhCha.Fortunate.Repositories.SqlSugar
{
    /// <summary>
    /// 生成使用
    /// </summary>
    public sealed class SqlSugarContext
    {
        private SqlSugarContext()
        {
        }

        public static SqlSugarClient GetInstance(DatabaseConfig config)
        {
            var entityConfig = new ConnectionConfig()
            {
                ConfigId = config.ConfigId,
                DbType = config.DbType.ToEnum<DbType>(),
                ConnectionString = config.ConnectionString,
                IsAutoCloseConnection = config.IsAutoCloseConnection,
            };
            //达梦数据库特殊配置
            if (config.DbType.ToEnum<DbType>() == DbType.Dm)
            {
                entityConfig.MoreSettings = new ConnMoreSettings()
                {
                    IsAutoToUpper = false //设置禁用自动转成大写表
                };
            }

            var instance = new SqlSugarClient(entityConfig);
            instance.Aop.OnLogExecuting = (sql, pars) =>
            {
                //Console.WriteLine(String.Concat("SQL：", sql));
            };
            return instance;
        }
    }
}
